package cn.shadow.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Utils {
    /**
     * 解析格式
     * cid=12&pid=13
     * 得到字符串map：{pid=13, cid=12}
     */
    public static Map<String, String> decodeAuth(String s) {
        Map<String, String> result = new HashMap<>();

        String name = null;
        int pos = 0; // Beginning of the unprocessed region
        int i; // End of the unprocessed region
        char c = 0; // Current character
        int length = s.length();
        for (i = 0; i < length; i++) {
            c = s.charAt(i);
            if (c == '=' && name == null) {
                if (pos != i) {
                    name = s.substring(pos, i);
                }
                pos = i + 1;
            } else if (c == '&') {
                if (name == null && pos != i) {
                    // We haven't seen an `=' so far but moved forward.
                    // Must be a param of the form '&a&' so add it with
                    // an empty value.
                    result.put(s.substring(pos, i), "");
                } else if (name != null) {
                    result.put(name, s.substring(pos, i));
                    name = null;
                }
                pos = i + 1;
            }
        }

        if (pos != i) { // Are there characters we haven't dealt with?
            if (name == null) { // Yes and we haven't seen any `='.
                result.put(s.substring(pos, i), "");
            } else { // Yes and this must be the last value.
                result.put(name, s.substring(pos, i));
            }
        } else if (name != null) { // Have we seen a name without value?
            result.put(name, "");
        }
        return result;
    }

    public static long int2Long(int var0, int var1) {
        return (long) var0 << 32 | (long) var1 & 4294967295L;
    }


    /**
     * map转成obj
     * 只能对javabean使用
     *
     * @param map
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> T Map2Bean(Map<String, Object> map, Class<T> cls) {
        T instance = null;
        try {
            instance = cls.newInstance();
            while (cls != Object.class) {
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    String name = field.getName();
                    Field type;
                    try {
                        type = cls.getDeclaredField(name);
                        if (type != null) {
                            type.setAccessible(true);
                            Object value = map.get(name);
                            if (value != null) {
                                type.set(instance, value);
                            }
                        }
                    } catch (NoSuchFieldException | IllegalAccessException e1) {
                        e1.printStackTrace();
                    }
                }
                cls = (Class<T>) cls.getSuperclass();
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return instance;
    }

    public static int random(int min, int max) {
        return (int) (Math.random() * max) + min;
    }

    public static int divide(int count, int maxCount) {
        return (count + maxCount - 1) / maxCount;
    }


    public static <V> V getValidObject(V[] array, int idx) {
        if (idx < 0) {
            return array[0];
        }

        if (idx >= array.length) {
            return array[array.length - 1];
        }

        return array[idx];
    }

    /**
     * 判断位置是否在矩形区域内
     */
    public static boolean isInRectangle(int minXIdx, int minYIdx, int maxXIdx, int maxYIdx, int xIdx,
                                  int yIdx) {
        return xIdx >= minXIdx && xIdx <= maxXIdx && yIdx >= minYIdx && yIdx <= maxYIdx;
    }

}
